// 品牌状态管理store
import { store } from "@/store";
import BrandAPI, { type Brand, type BrandFormData, type BrandQueryForm } from "@/api/goods/brand";

/**
 * 创建品牌管理store
 * 包含品牌列表和操作品牌的方法
 */
const useBrandStore = defineStore({
  id: "brand",
  state: () => ({
    brandList: [] as Brand[],
    currentBrand: null as Brand | null,
    loading: false,
  }),
  actions: {
    // 获取品牌列表
    async fetchBrandList(params?: BrandQueryForm) {
      this.loading = true;
      try {
        const response = await BrandAPI.getPage(params);
        this.brandList = response;
      } finally {
        this.loading = false;
      }
    },

    // 创建品牌
    async createBrand(data: BrandFormData) {
      await BrandAPI.create(data);
      await this.fetchBrandList();
    },

    // 更新品牌
    async updateBrand(id: number, data: BrandFormData) {
      await BrandAPI.update(id, data);
      await this.fetchBrandList();
    },

    // 删除品牌
    async deleteBrand(id: number) {
      await BrandAPI.delete(id);
      await this.fetchBrandList();
    },
  },
});

// 导出store
const useBrandStoreHook = () => {
  return useBrandStore(store);
};

export default useBrandStoreHook;
